package fhp.hlhj.giantfold.activity.red
import android.animation.Animator
import android.animation.ObjectAnimator
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import android.widget.LinearLayout
import android.widget.Toast
import com.google.gson.Gson
import com.lzy.okgo.model.HttpParams
import fhp.hlhj.giantfold.BaseActivity
import fhp.hlhj.giantfold.Contents
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.adapter.ShakeRedListAdp
import fhp.hlhj.giantfold.customView.ShakeRedDialog
import fhp.hlhj.giantfold.event.JpushEvent
import fhp.hlhj.giantfold.interfaces.IConfig
import fhp.hlhj.giantfold.interfaces.IShakeRed
import fhp.hlhj.giantfold.interfaces.IUser
import fhp.hlhj.giantfold.javaBean.*
import fhp.hlhj.giantfold.presenter.ConfigPresenter
import fhp.hlhj.giantfold.presenter.ShakeRedPresenter
import fhp.hlhj.giantfold.presenter.UserPresenter
import fhp.hlhj.giantfold.utils.MyUtils
import fhp.hlhj.giantfold.utils.ShakeUtils
import kotlinx.android.synthetic.main.activity_shake_red_aty.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.ArrayList

class ShakeRedAty : BaseActivity(), ShakeUtils.OnShakeListener, IShakeRed,IUser,IConfig {
    override fun onResp(configBean: ConfigBean) {
        if (configBean.code==200){
            cutPoint=configBean.data.shake_one_consume_points.toInt()
        }
    }
    private  var cutPoint=0
    private  var point=0
    private lateinit var adapter:ShakeRedListAdp
    private lateinit var shakeRedPresenter:ShakeRedPresenter
    private lateinit var userPresenter:UserPresenter
    private lateinit var configPresenter:ConfigPresenter
    private var page=0
    private val datas=ArrayList<ShakeRedListBean.DataBean>()
    override fun getContentId(): Int = R.layout.activity_shake_red_aty
    private var enable=true
    private lateinit var shakeUtils1 :ShakeUtils
    override fun initView() {
        configPresenter=ConfigPresenter(this)
        val configHP = HttpParams()
        configHP.put("keys","[\"shake_one_consume_points\"]")
        configPresenter.getConfig(configHP)
        adapter= ShakeRedListAdp(R.layout.shake_list_item,datas)
        shakeUtils1=ShakeUtils(this)
        shakeRedPresenter= ShakeRedPresenter(this)
        userPresenter= UserPresenter(this)
        val pa = HttpParams()
        pa.put("user_id",Contents.userId)
        userPresenter.getUser(pa)

        val params = HttpParams()
        params.put("user_id",Contents.userId)
        params.put("page",page)
        shakeRedPresenter.shakeRedList(params)
        shakeRedPresenter.setRecycler()
    }

    override fun initListener() {
        btRule.setOnClickListener {

/*            val element = datas[0]
            element.nick_name="3"
            datas.add(0, element)
            adapter.notifyItemInserted(0)
            listView.scrollToPosition(0)*/
        }
        shakeUtils1.setOnShakeListener(this)
        btExit.setOnClickListener {
            finish()
        }
        img.setOnClickListener {
            if (enable){
                enable=false
                val animator = ObjectAnimator.ofFloat(img, "rotation", 0f, 30f, -30f,0f).setDuration(500)
                animator.repeatCount=2
                img.pivotX= (img.width/2).toFloat()
                img.pivotY=img.height.toFloat()/2
                animator.start()

                animator.addListener(object :Animator.AnimatorListener{
                    override fun onAnimationRepeat(p0: Animator?) {
                    }

                    override fun onAnimationEnd(p0: Animator?) {
                        val params = HttpParams()
                        params.put("user_id",Contents.userId)
                        shakeRedPresenter.shakeRed(params)
                        enable=true
                    }

                    override fun onAnimationCancel(p0: Animator?) {
                    }

                    override fun onAnimationStart(p0: Animator?) {
                    }
                })
            }
        }
    }
    override fun onResume() {
        super.onResume()
        shakeUtils1.onResume()
    }
    override fun onPause() {
        super.onPause()
        shakeUtils1.onPause()
    }
    override fun onShake() {
        if (enable){
            enable=false
            val animator = ObjectAnimator.ofFloat(img, "rotation", 0f, 30f, -30f,0f).setDuration(500)
            animator.repeatCount=2
            img.pivotX= (img.width/2).toFloat()
            img.pivotY=img.height.toFloat()/2
            animator.start()

            animator.addListener(object :Animator.AnimatorListener{
                override fun onAnimationRepeat(p0: Animator?) {
                }

                override fun onAnimationEnd(p0: Animator?) {
                    val params = HttpParams()
                    params.put("user_id",Contents.userId)
                    shakeRedPresenter.shakeRed(params)
                    enable=true
                }

                override fun onAnimationCancel(p0: Animator?) {
                }

                override fun onAnimationStart(p0: Animator?) {
                }
            })
        }

    }

    override fun showLoading() {
    }

    override fun onResp(shakeRedBean: ShakeRedBean) {
        if (shakeRedBean.code==200){
            progressBar.setProgress(point-cutPoint)
            tvProgress.text=(point-cutPoint).toString()
            val dialog = ShakeRedDialog(this, shakeRedBean.data.annual_rate)
            dialog.show()
        }else{
            MyUtils.toast(shakeRedBean.msg)
        }
    }

    override fun onListResp(shakeRedListBean: ShakeRedListBean) {
        if (shakeRedListBean.code==200){
            datas.addAll(shakeRedListBean.data)
            adapter.notifyDataSetChanged()
        }
    }

    override fun setRecycler() {
        listView.layoutManager=LinearLayoutManager(this,LinearLayout.VERTICAL,false)
        listView.adapter=adapter
        listView.itemAnimator=DefaultItemAnimator()
    }
    @Subscribe(threadMode = ThreadMode.POSTING)
    fun recieveJpush(jpushEvent: JpushEvent){
        val jPushBean = Gson().fromJson(jpushEvent.extraStr, JPushBean::class.java)
        if (jPushBean.page.equals("yyy")){
            val dataBean = ShakeRedListBean.DataBean()

            dataBean.nick_name=jPushBean.nick_name
            dataBean.user_name=jPushBean.nick_name
            dataBean.annual_rate=jPushBean.annual_rate.toString()
            dataBean.savename=jPushBean.savename
            dataBean.savepath=jPushBean.savepath

            datas.add(0,dataBean)
            adapter.notifyItemInserted(0)
            listView.scrollToPosition(0)
        }
    }

    override fun getUser(userInfoBean: UserInfoBean) {
        if (userInfoBean.code==200){
            point=userInfoBean.data.physical_point.toInt()
            progressBar.setProgress(point)
            tvProgress.text=userInfoBean.data.physical_point
        }
    }
}
